//
//  MainTabBar.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/10.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.addSubview(plusBtn)
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "hidenPlusBtn", name: HidenPlusBtn, object: nil)
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    func hidenPlusBtn() {
        plusBtn.enabled = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //调整tabBar的子控件布局
        var index: CGFloat = 0
        let w = self.bounds.width / 5
        let h = self.bounds.height

        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subview.frame = CGRect(x: w * index, y: 0, width: w, height: h)
                
                index += (index == 1 ? 2 : 1)
            }
            //设置加号的frame
            plusBtn.frame = CGRect(x: 2 * w, y: 0, width: w, height: h)
        }
    }
    
    lazy var plusBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
        return btn
    }()

}
